package edu.pku.sei.transformation.qvt.adapter;

import java.util.ArrayList;
import java.util.List;

import edu.pku.sei.transformation.editor.model.AtomicTransformation;
import edu.pku.sei.transformation.editor.model.Key;
import edu.pku.sei.transformation.editor.model.Rule;
import edu.pku.sei.transformation.editor.model.TransformationFormalParameter;
import edu.pku.sei.transformation.editor.model.constraint.Constraint;
import edu.pku.sei.transformation.qvt.rule.IKey;
import edu.pku.sei.transformation.qvt.rule.IRelation;
import edu.pku.sei.transformation.qvt.rule.ITransformation;
import edu.pku.sei.transformation.qvt.rule.ITypedModel;

public class TransformationAdapter implements ITransformation {
	public AtomicTransformation element;
	protected List<IRelation> relations;
	public TransformationAdapter(AtomicTransformation t) {
		this.element = t;
		relations = new ArrayList<IRelation>();
		for (Object obj : t.getRules()) {
			if (obj instanceof Rule) {
				IRelation r = new RelationAdapter((Rule) obj);
				r.setOwner(this);
				relations.add(r);
			}
		}
	}
	
	@Override
	public List<IKey> getKeys() {
		List<IKey> list = new ArrayList<IKey>();
		for (Object k : element.getKey()) {
			list.add(new KeyAdapter((Key)k));
		}
		return list;
	}

	@Override
	public String getName() {
		return element.getName();
	}

	@Override
	public List<IRelation> getRelation() {
		return relations;
	}

	@Override
	public List<ITypedModel> getTypedModel() {
		ArrayList<ITypedModel> typedModels = new ArrayList<ITypedModel>();
		for (TransformationFormalParameter p : element.getModelParameter()) {
			ITypedModel typeModel = new TypedModelAdapter(p.getTypedModel());
			typedModels.add(typeModel);
		}
		return typedModels;
	}
	
	@Override
	public IRelation getRelationByName(String name) {
		for (IRelation relation : relations) {
			if (relation.getName().equals(name)) {
				return relation;
			}
		}
		return null;
	}

	@Override
	public List<String> getQuerys() {
		ArrayList<String> list = new ArrayList<String>();
		for (Constraint c : element.getConstraints()) {
			if (c.getContent().startsWith("query"))
				list.add(c.getContent());
		}
		return list;
	}

}
